layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 我的签到管理
     */
    var EduCourseSelectionInfo = {
        tableId: "eduCourseSelectionInfoSignManagerTable"
    };

    /**
     * 初始化表格的列
     */
    EduCourseSelectionInfo.initColumn = function () {
        return [[
            {field: 'uniqueKey', hide: true, title: ''},
            {field: 'curriculumId',hide:true, sort: true, title: '课程编号'},
            {field: 'studentId', sort: true, title: '学号'},
            {field: 'studentName', sort: true, title: '学生姓名'},
            {align: 'center', field: 'curriculumLink', sort: true, title: '课程名称'},
            {field: 'teacherId',hide:true, sort: true, title: '教师编号'},
            {field: 'teacherName', sort: true, title: '教师姓名'},
            {field: 'creditHour', sort: true, title: '学分'},
            {field: 'timeForClass', sort: true, title: '上课时间'},
            {field: 'classroom', sort: true, title: '教室'},
            {field: 'signTime', title: '签到时间'},
            {align: 'center', field: 'curriculumIcon', title: '签到状态',templet:function (d) {
                    var e = d.status;
                    if(e==1){
                        e = '<a style="color: #0000ff">已签到</a>';
                        return e;
                    }
                    if(e==2){
                        e = '<a style="color: #0000ff">未签到</a>';
                        return e;
                    }
                    if(e==3){
                        e = '<a style="color: #0000ff">请假</a>';
                        return e;
                    }
                    if(e==4){
                        e = '<a style="color: #0000ff">退签</a>';
                        return e;
                    }
                }},
            {align: 'center', field: 'curriculumIcon', title: '签到码'},
            {align: 'center', toolbar: '#tableBar', title: '操作',minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    EduCourseSelectionInfo.search = function () {
        var queryData = {};
        queryData['curriculumName'] = $("#curriculumName").val();
        table.reload(EduCourseSelectionInfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 点击请假
     *
     * @param data 点击按钮时候的行数据
     */
    EduCourseSelectionInfo.onAsk = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/eduCourseSelectionInfo/sign/edit", function (data) {
            Feng.success("成功!");
            table.reload(EduCourseSelectionInfo.tableId);
        }, function (data) {
            Feng.error("失败!" + data.responseJSON.message + "!");
        });
        ajax.set("uniqueKey", data.uniqueKey);
        ajax.set("status","3");
        ajax.start();
    };

    EduCourseSelectionInfo.onMend = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/eduCourseSelectionInfo/sign/edit", function (data) {
            Feng.success("成功!");
            table.reload(EduCourseSelectionInfo.tableId);
        }, function (data) {
            Feng.error("失败!" + data.responseJSON.message + "!");
        });
        ajax.set("uniqueKey", data.uniqueKey);
        ajax.set("status","1");
        ajax.start();
    };

    EduCourseSelectionInfo.onReruen = function (data) {
        var ajax = new $ax(Feng.ctxPath + "/eduCourseSelectionInfo/sign/edit", function (data) {
            Feng.success("成功!");
            table.reload(EduCourseSelectionInfo.tableId);
        }, function (data) {
            Feng.error("失败!" + data.responseJSON.message + "!");
        });
        ajax.set("uniqueKey", data.uniqueKey);
        ajax.set("status","4");
        ajax.start();
    };

    var tableHeigth = $(document).height();
    var lineHeight = $(".layui-form-item").height();
    var tableLine = tableHeigth / (lineHeight + 4);
    var lineNum = parseInt(tableLine) / 5 ;
    lineNum = Math.max((parseInt(lineNum) * 5), 5);
    
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + EduCourseSelectionInfo.tableId,
        url: Feng.ctxPath + '/eduCourseSelectionInfo/sign/list',
        page: true,
        limit: lineNum,
        limits: [10,15,20,25,30,50,100],
        defaultToolbar:['filter', 'print'],
        toolbar: true,
        even: true,        
        height: "full-98",
        cellMinWidth: 100,
        cols: EduCourseSelectionInfo.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        EduCourseSelectionInfo.search();
    });

    $('#btnSetAbsent').click(function () {
        var selectedCourse = $('#courseSelect').val(); // 获取选中的课程

        // 检查是否选择了课程
        if (selectedCourse) {
            // 发送 AJAX 请求
            $.ajax({
                url:Feng.ctxPath + "/eduCourseSelectionInfo/sign/reset", // 替换为实际的后端处理 URL
                type: 'POST',
                data: { course: selectedCourse }, // 发送选中的课程信息
                success: function(response) {
                    // 请求成功时的处理逻辑
                    alert('签到状态已重置: ' + response);
                    table.reload(EduCourseSelectionInfo.tableId);
                },
                error: function(xhr, status, error) {
                    // 请求失败时的处理逻辑
                    alert('重置请求失败: ' + error);
                }
            });
        } else {
            // 如果没有选择课程，则显示提示
            alert('请选择一个课程');
        }
    });

    // 工具条点击事件
    table.on('tool(' + EduCourseSelectionInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'mend') {
            EduCourseSelectionInfo.onMend(data);
        } else if (layEvent === 'ask') {
            EduCourseSelectionInfo.onAsk(data);
        }
        else if (layEvent === 'onReturn') {
            EduCourseSelectionInfo.onReruen(data);
        }
    });
});
